package com.example.aidldemo.aidl

import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.Bundle
import android.os.IBinder
import android.os.RemoteException
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.example.aidldemo.databinding.ActivityMainBinding
import com.example.aidldemo.server.BookManagerService

/**
 * @author: njb
 * @date:   2025/3/22 19:27
 * @desc:   描述
 */
class BookTestActivity :AppCompatActivity(){
    private val TAG: String = "TestActivity"
    private var bookManager: IBookManager? = null
    private lateinit var binding:ActivityMainBinding
    private val mConnection: ServiceConnection = object : ServiceConnection {
        override fun onServiceConnected(name: ComponentName, service: IBinder) {
            bookManager = IBookManager.Stub.asInterface(service)
            try {
                bookManager?.let {
                    val books = it.getBookList()
                    if (books != null && books.isNotEmpty()) {
                        Log.d(TAG, " -- onServiceConnected -- book:$books")
                    }
                    it.registerListener(clientListener)
                }

            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }

        override fun onServiceDisconnected(name: ComponentName) {
            try {
                bookManager?.unregisterListener(clientListener)
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }
    }

    private val clientListener: IOnNewBookAddListener = object : IOnNewBookAddListener.Stub() {
        @Throws(RemoteException::class)
        override fun basicTypes(
            anInt: Int,
            aLong: Long,
            aBoolean: Boolean,
            aFloat: Float,
            aDouble: Double,
            aString: String
        ) {
        }

        @Throws(RemoteException::class)
        override fun onNewBookAdd(newBook: Book) {
            Log.d(TAG, " -- onNewBookAdd -- newBook:$newBook")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.tvBind.setOnClickListener{
            val intent = Intent(this@BookTestActivity, BookManagerService::class.java)
            bindService(intent, mConnection, BIND_AUTO_CREATE)
        }
        binding.tvGet.setOnClickListener{
            try {
                val books = bookManager!!.bookList
                if (books != null && books.isNotEmpty()) {
                    Log.d(TAG, " -- onClick -- books:$books")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        binding.tvAdd.setOnClickListener{
            try {
                bookManager?.let {
                    it.addNewBook(Book(1003, "计算机组成原理"))
                    it.addNewBook(Book(1004, "操作系统"))
                }
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (bookManager != null && bookManager!!.asBinder().isBinderAlive) {
            try {
                bookManager?.unregisterListener(clientListener)
            } catch (e: RemoteException) {
                e.printStackTrace()
            }
        }
        unbindService(mConnection)
    }
}